package com.ruoyi.mespro.controller;

import java.text.ParseException;
import java.util.*;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mespro.domain.*;
import com.ruoyi.mespro.service.IAlldataSizingService;
import com.ruoyi.mespro.service.IShaftDefectService;
import com.ruoyi.mespro.service.IShaftJarService;
import com.ruoyi.project.domain.OrderInfo;
import com.ruoyi.project.service.OrderInfoService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.xmlbeans.impl.xb.xsdschema.All;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mespro.service.IShaftSizeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 浆纱数据汇总Controller
 *
 * @author Jun Xiang
 * @date 2022-06-07
 */
@RestController
@RequestMapping("/mesana/size")
public class ShaftSizeController extends BaseController
{
    @Autowired
    private IShaftSizeService shaftSizeService;
    @Autowired
    private IAlldataSizingService alldataSizingService;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private IShaftJarService shaftJarService;
    @Autowired
    private IShaftDefectService shaftDefectService;

    /**
     * 查询浆纱数据汇总列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ShaftSize shaftSize)
    {
        startPage();
        List<ShaftSize> list = shaftSizeService.selectShaftSizeList(shaftSize);
        return getDataTable(list);
    }

    /**
     * 导出班次
     */
    @Log(title = "浆纱数据班次报表", businessType = BusinessType.EXPORT)
    @GetMapping("/export_banci")
    public AjaxResult export_banci(ShaftSize shaftSize)
    {
        List<SizeShafttypeReport> list = shaftSizeService.selectSizeShiftReport(shaftSize);
        ExcelUtil<SizeShafttypeReport> util = new ExcelUtil<SizeShafttypeReport>(SizeShafttypeReport.class);
        return util.exportExcel(list, "浆纱数据班次报表");
    }

    /**
     * 班次报表
     */
    @RequestMapping(value = "bancireport", method = RequestMethod.GET)
    public TableDataInfo bancireport(ShaftSize shaftSize){
        startPage();
        List<SizeShafttypeReport> list = shaftSizeService.selectSizeShiftReport(shaftSize);
        return getDataTable(list);
    }

    /**
     * 品种报表
     */
    @RequestMapping(value = "pdtreport", method = RequestMethod.GET)
    public TableDataInfo pdtreport(ShaftSize shaftSize){
        startPage();
        List<SizePdfReport> list = shaftSizeService.selectWarpPdfReport(shaftSize);
        return getDataTable(list);
    }

    /**
     * 根据轴号获取实时表中的织轴长度 用于落轴时的弹出提示
     */
    @RequestMapping(value = "/getCurlength/{shaftCode}", method = RequestMethod.GET)
    public AjaxResult getCurlength(@PathVariable("shaftCode") String shaftCode)
    {
        AlldataSizing alldataSizing = alldataSizingService.selectAlldataSizingByShaftCode(shaftCode);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("curlength", alldataSizing.getBeamlength());
        return AjaxResult.success(resultMap);
    }


    @RequestMapping(value = "/getMacList", method = RequestMethod.GET)
    public AjaxResult getMacList(){Map<String, Object> resultMap = new HashMap<>();
        AlldataSizing alldataSizing = new AlldataSizing();
        List<AlldataSizing> list = alldataSizingService.selectAlldataSizingList(alldataSizing);
        ArrayList<String> maclist = new ArrayList<>();
        for (AlldataSizing a: list) {
            if (a.getZzno()!=null && a.getZzno()!="") {
                maclist.add(a.getMid() + "-" + a.getStation());
            }
        }
        resultMap.put("maclist", maclist);
        return AjaxResult.success(resultMap);

    }

    @RequestMapping(value = "/getMachine/{maccode}", method = RequestMethod.GET)
    public AjaxResult getMachine(@PathVariable("maccode") String maccode){
        String mid = maccode.split("-")[0];
        String station = maccode.split("-")[1];
        AlldataSizing alldataSizing = new AlldataSizing();
        alldataSizing.setMid(mid);
        alldataSizing.setStation(station);
        List<AlldataSizing> list = alldataSizingService.selectAlldataSizingList(alldataSizing);
        Map<String, Object> resultMap = new HashMap<>();
        if (list.size()>0){
            AlldataSizing al = list.get(0);
            resultMap.put("machine", al);
            ShaftSize shaftSize = shaftSizeService.selectShaftSizeByShaftCode(al.getZzno());
            resultMap.put("shaftSize", shaftSize);
        }

        return AjaxResult.success(resultMap);

    }


    /**
     * 获取浆纱机的机器列表, ERP品种信息列表, 当天织轴数量
     */
    @RequestMapping(value = "/getInitData", method = RequestMethod.GET)
    public AjaxResult getmaclist(){
        Map<String, Object> resultMap = new HashMap<>();
        AlldataSizing alldataSizing = new AlldataSizing();
        alldataSizing.setZzno("00");
        List<AlldataSizing> list = alldataSizingService.selectAlldataSizingList(alldataSizing);
        ArrayList<String> maclist = new ArrayList<>();
        for (AlldataSizing a: list) {
            maclist.add(a.getMid() + "-" + a.getStation());
        }
        resultMap.put("maclist", maclist);
        List<OrderInfo> orderlist = orderInfoService.selectOrderInfoList();
        resultMap.put("orderlist", orderlist);
        ShaftSize sw = shaftSizeService.selectTodayLastRecord();
        if (sw == null){
            resultMap.put("todayNum", 0);
        }else {
            String shaftcode = sw.getShaftcode();
            shaftcode = shaftcode.split("-")[0];
            int todayNum = Integer.parseInt(shaftcode.substring(shaftcode.length()-3, shaftcode.length()));
            resultMap.put("todayNum", todayNum);

        }
        return AjaxResult.success(resultMap);
    }
    /**
     * 落轴操作
     */
    @RequestMapping(value = "/luozhouoperation/{id}", method = RequestMethod.POST)
    public AjaxResult luozhouoperation(@PathVariable("id") Long id)
    {
        ShaftSize shaftSize = shaftSizeService.selectShaftSizeById(id);
        shaftSize.setBeamstatus(1);
        shaftSize.setLjtime(new Date());
        AlldataSizing alldataSizing = new AlldataSizing();
        alldataSizing.setMid(shaftSize.getMaccode().split("-")[0]);
        alldataSizing.setStation(shaftSize.getMaccode().split("-")[1]);
        AlldataSizing updateAS = alldataSizingService.selectAlldataSizingByMacCode(alldataSizing);
        shaftSize.setAlldataSizing(updateAS);
        if (shaftSize.getIscrossshift()==1){
            ShaftSize lastshaftsize = shaftSizeService.selectShaftSizeByCodeStatus(shaftSize.getShaftcode());
            shaftSize.setBeamlength(updateAS.getBeamlength()-lastshaftsize.getBeamlength());
        }
        alldataSizingService.updateZZno(updateAS);
        return AjaxResult.success(shaftSizeService.updateShaftSize(shaftSize));
    }

    /**
     * 找到上一个轴
     */
    @RequestMapping(value = "/selectNewestZhou/{maccode}", method = RequestMethod.GET)
    public AjaxResult selectNewestZhou(@PathVariable("maccode") String maccode)
    {
        ShaftSize shaftSize = shaftSizeService.selectLastShaftSize(maccode);
        return AjaxResult.success(shaftSize);
    }


    /**
     * 导出浆纱数据汇总列表
     */
    @Log(title = "浆纱数据汇总", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ShaftSize shaftSize)
    {
        List<ShaftSize> list = shaftSizeService.selectShaftSizeList(shaftSize);
        ExcelUtil<ShaftSize> util = new ExcelUtil<ShaftSize>(ShaftSize.class);
        return util.exportExcel(list, "浆纱数据汇总数据");
    }

    /**
     * 获取浆纱数据汇总详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(shaftSizeService.selectShaftSizeById(id));
    }

    /**
     * 新增浆纱数据汇总
     */
    @Log(title = "浆纱数据汇总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShaftSize shaftSize) throws ParseException {
        shaftSize.setBeamstatus(0);
        shaftSize.setIscrossshift(0);
        shaftSize.setSjtime(new Date());
        List<SysDictData> data = dictTypeService.selectDictDataByType("dict_shiftType");
        SysDictData sysDictData = DateUtils.parseHourToShiftType(new Date(), data);
        shaftSize.setShiftdate(sysDictData.getCreateTime());
        shaftSize.setShifttype(Integer.parseInt(sysDictData.getDictValue()));
        ShaftSize shaftSize1 = shaftSizeService.selectShaftSizeByShaftCode(shaftSize.getShaftcode());
        if (shaftSize1 != null){
            return toAjax(0);
        }


        AlldataSizing alldataSizing = new AlldataSizing();
        alldataSizing.setMid(shaftSize.getMaccode().split("-")[0]);
        alldataSizing.setStation(shaftSize.getMaccode().split("-")[1]);
               AlldataSizing updateAS = alldataSizingService.selectAlldataSizingByMacCode(alldataSizing);

        updateAS.setZzno(shaftSize.getShaftcode());
        alldataSizingService.updateAlldataSizing(updateAS);
        ShaftJar shaftJar = new ShaftJar();
        shaftJar.setJarno(shaftSize.getJarNo());
        List<ShaftJar> jarlist = shaftJarService.selectShaftJarList(shaftJar);
        if (jarlist.size() == 0){
            shaftJarService.insertShaftJar(shaftJar);
        }
        ShaftDefect shaftDefect = new ShaftDefect();
        shaftDefect.setJarno(shaftSize.getJarNo());
        shaftDefect.setShaftcode(shaftSize.getShaftcode());
        List<ShaftDefect> defectList = shaftDefectService.selectShaftDefectList(shaftDefect);
        if (defectList.size() == 0){
            shaftDefectService.insertShaftDefect(shaftDefect);
        }

        return toAjax(shaftSizeService.insertShaftSize(shaftSize));
    }

    /**
     * 修改浆纱数据汇总
     */
    @Log(title = "浆纱数据汇总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShaftSize shaftSize)
    {
        return toAjax(shaftSizeService.updateShaftSize(shaftSize));
    }

    /**
     * 删除浆纱数据汇总
     */
    @Log(title = "浆纱数据汇总", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shaftSizeService.deleteShaftSizeByIds(ids));
    }
}
